package com.haoshuang.zhouzhoubang.ui.page.shop.order.child

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.FragmentOrderListShopBinding
import com.haoshuang.zhouzhoubang.model.bean.OrderBean8
import com.haoshuang.zhouzhoubang.ui.page.shop.order.ShopOrderActivity
import com.haoshuang.zhouzhoubang.ui.page.shop.order.detail.ShopOrderDetailActivity
import com.mufeng.libs.base.BaseFragment
import com.mufeng.libs.core.setEmptyData
import com.mufeng.libs.core.setOnItemChildClick
import com.mufeng.libs.core.setupData
import com.mufeng.libs.core.setupEmptyData
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast

/**
 *
 */
class OrderListShopFragment : BaseFragment<OrderListShopViewModel, FragmentOrderListShopBinding>() {

    companion object {
        fun getInstance(status: Int): OrderListShopFragment {
            val fragment = OrderListShopFragment()
            fragment.arguments?.putInt("status", status)
            return fragment
        }
    }

    private val status by lazy { arguments?.getInt("status", -1) ?: -1 }

    override fun initView(savedInstanceState: Bundle?) {

    }

    var list = arrayListOf<OrderBean8>()
    var adapter = ShopOrderListAdapter(list)
    var p = 1
    override fun initData() {
        var act = activity as ShopOrderActivity
        binding.refreshLayout.setOnRefreshListener {
            p = 1
            getNet()
        }
        binding.refreshLayout.setOnLoadMoreListener {
            p++
            getNet()
        }
        p = 1
        getNet()
        binding.apply {
            adapter = ShopOrderListAdapter(list)
            rvList.layoutManager = LinearLayoutManager(requireContext())
            rvList.adapter = adapter
            adapter.setEmptyData()
            adapter.setOnItemClickListener { adapter, view, position ->
                startPage<ShopOrderDetailActivity>("ids" to list[position].id)
            }

            adapter.setOnItemChildClick(id = R.id.tvRightButton) { view, position, item ->
                if (list[position].status == 0) {
                    act.Pay(list[position].id)
                } else {
                    vm.getData3("id" to list[position].id)
                }
            }
            adapter.setOnItemChildClick(id = R.id.tvLeftButton) { view, position, item ->
                vm.getData2("id" to list[position].id)
            }
        }
    }

    private fun getNet() {
        vm.getData("status" to status, "page" to p)
    }

    override fun lazyLoadData() {

    }

    override fun startObserve() {
        vm.getData2.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                if (code != "402") {
                    toast(msg)
                }
            }
            onSuccess {
                dismissLoading()
                p = 1
                getNet()
            }
        }
        vm.getData3.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                if (code != "402") {
                    toast(msg)
                }
            }
            onSuccess {
                dismissLoading()
                p = 1
                getNet()
            }
        }
        vm.getData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                if (code != "402") {
                    toast(msg)
                }
                binding.refreshLayout.setupEmptyData(list, adapter, p)
            }
            onSuccess {
                dismissLoading()
                binding.refreshLayout.setupData(it, list, adapter, p)
            }
        }

    }

    fun updata() {
        p = 1
        getNet()
    }

}